#region Copyright Notice

// 
// 
//  
//  Copyright (c) 2009-2011 Anindya Chatterjee
//  
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//  
//      http://www.apache.org/licenses/LICENSE-2.0
//  
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// 
// 

#endregion

using System;
using System.Drawing;
using System.Linq;

namespace AbstractClass.IO
{
    /// <summary>
    /// Represents a system registered file type.
    /// </summary>
    public class FileTypeInfo
    {
        /// <summary>
        /// Gets or sets the file extension.
        /// </summary>
        /// <value>The file extension.</value>
        public string FileExtension { get; internal set; }

        /// <summary>
        /// Gets or sets the type of the file.
        /// </summary>
        /// <value>The type of the file.</value>
        public string FileType { get; internal set; }

        /// <summary>
        /// Gets or sets the file icon.
        /// </summary>
        /// <value>The file icon.</value>
        public FileIconInfo IconInfo { get; internal set; }
    }

    /// <summary>
    /// Provides instance methods to extract the icon information associated with a registered file type.
    /// </summary>
    public class FileIconInfo
    {
        /// <summary>
        /// Gets or sets the icon source.
        /// </summary>
        /// <value>The icon source.</value>
        public string IconSource { get; internal set; }

        /// <summary>
        /// Gets or sets the index of the icon in the source file.
        /// </summary>
        /// <value>The index of the icon.</value>
        public int IconIndex { get; internal set; }

        /// <summary>
        /// Gets the icon associated with the registered file type.
        /// </summary>
        /// <returns>The icon associated with the file type.</returns>
        public Icon GetFileIcon()
        {
            var hDummy = new[] {IntPtr.Zero};
            var hIconEx = new[] {IntPtr.Zero};
            try
            {
                uint readIconCount = NativeMethods.ExtractIconEx(IconSource, IconIndex, hIconEx, hDummy, 1);
                if (readIconCount > 0 && hIconEx[0] != IntPtr.Zero)
                {
                    var extractedIcon = (Icon) Icon.FromHandle(hIconEx[0]).Clone();
                    return extractedIcon;
                }
                else
                    return null;
            }
            finally
            {
                foreach (IntPtr ptr in hIconEx.Where(ptr => ptr != IntPtr.Zero))
                {
                    NativeMethods.DestroyIcon(ptr);
                }

                foreach (IntPtr ptr in hDummy.Where(ptr => ptr != IntPtr.Zero))
                {
                    NativeMethods.DestroyIcon(ptr);
                }
            }
        }
    }
}